package com.web.controller;

import com.alibaba.fastjson.JSONArray;
import com.jfinal.core.Controller;
import com.jfinal.upload.UploadFile;
import com.oreilly.servlet.Base64Decoder;
import com.web.model.CardLaunchRecord;
import org.apache.log4j.Logger;
import sun.misc.BASE64Decoder;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Created by reeveslee on 16/6/29.
 */
public class CardController extends Controller {
    private static final Logger LOGGER = Logger.getLogger(CardController.class);

    /**
     * 提交打卡信息
     */
    public void submit() {
        int userId = (int) (long) getSession().getAttribute("userId");

        String uploadSrc_1 = getPara("uploadSrc_1");
        String uploadSrc_2 = getPara("uploadSrc_2");
        String uploadSrc_3 = getPara("uploadSrc_3");
        JSONArray uploadSrc = new JSONArray();
        uploadSrc.add(uploadSrc_1);
        uploadSrc.add(uploadSrc_2);
        uploadSrc.add(uploadSrc_3);


        int day = getParaToInt("day"); // 日期

        int way = getParaToInt("way"); // 类型
        String text = getPara("text"); // 心情
        String slogan = getPara("slogan"); // 个性签名
        String weather = getPara("weather");

        int launchInfoId = 0;

        if (way == 0 || way == 2) {
            double mile = Double.parseDouble(getPara("mile")); // 里程数
            String wayStr = (way == 0) ? "跑步" : "骑行";
            Calendar calendar = Calendar.getInstance();
            calendar.add(Calendar.DAY_OF_YEAR, day);

            launchInfoId = (int) CardLaunchRecord.cardLaunchRecordDao.addLaunchRecordMile(userId, uploadSrc, mile, wayStr, calendar.getTime(), weather, text);

        } else if (way == 1) {
            int step = getParaToInt("step"); // 步数
            String wayStr = "走路";
            Calendar calendar = Calendar.getInstance();
            calendar.add(Calendar.DAY_OF_YEAR, day);

            launchInfoId = (int) CardLaunchRecord.cardLaunchRecordDao.addLaunchRecordStep(userId, uploadSrc, step, wayStr, calendar.getTime(), weather, text);

        } else {
            double time = Double.parseDouble(getPara("time")); // 运动时长
            String wayStr = (way == 3) ? "健身" : "其他";
            Calendar calendar = Calendar.getInstance();
            calendar.add(Calendar.DAY_OF_YEAR, day);

            launchInfoId = (int) CardLaunchRecord.cardLaunchRecordDao.addLaunchRecordTime(userId, uploadSrc, time, wayStr, calendar.getTime(), weather, text);
        }

        redirect("/route/gotoShare/" + launchInfoId);
    }

    public void deleteRecord() {
        int launchId = getParaToInt("launchId");
        CardLaunchRecord.cardLaunchRecordDao.findById(launchId).delete();
        setAttr("result", true);
        renderJson();
    }
}
